package com.zhudeshuai.zhudemolist.widget.MyScrollView;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.IntDef;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PagerSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;

/**
 * @author zhudeshuai
 * @since 17/12/6
 */

public class MyScrollView extends ScrollView {

	//头部最小高度
	private int mHeaderMinHeight = 300;

	//头部最大高度
	private int mHeaderMaxHeight = 900;

	//头部view
	private RecyclerView mHeaderView;

	//最外层内容布局
	private LinearLayout mContentLayout;

	//手指移动上一次Y坐标
	private float mLastY;

	//ScrollView是否滚动到顶部
	private boolean isScrollToTop = true;

	//头部降低中
	private boolean mHeaderReducing;

	//头部增高中
	private boolean mHeaderAdding;

	//滚动监听
	private MyScrollListener mScrollListener;

	//当前状态
	private @Status int mCurrentStatus;


	public MyScrollView(Context context) {
		this(context, null);
	}

	public MyScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);

		setMotionEventSplittingEnabled(false);

		initView();
	}

	private void initView() {
		//添加最外层布局
		mContentLayout = new LinearLayout(getContext());
		mContentLayout.setOrientation(LinearLayout.VERTICAL);
		addView(mContentLayout, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

		//1.添加头部RecyclerView
		mHeaderView = new RecyclerView(getContext());
		mHeaderView.setBackgroundColor(Color.GREEN);
		LinearLayoutManager layout = new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false);
		mHeaderView.setLayoutManager(layout);
		mContentLayout.addView(mHeaderView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mHeaderMinHeight));

		setHeaderViewPager(mHeaderView, 0);

	}

	/**
	 * 设置RecyclerView为viewPager样式
	 *
	 * @param recyclerView
	 */
	private void setHeaderViewPager(RecyclerView recyclerView, int type) {
		if (type == 1) {
			PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
			pagerSnapHelper.attachToRecyclerView(recyclerView);
		} else {
			MySnapHelper snapHelper = new MySnapHelper();
			snapHelper.attachToRecyclerView(recyclerView);
		}
	}

	@Override
	public void addView(View child) {
		if (mContentLayout != null) {
			mContentLayout.addView(child);
		}
	}

	@Override
	public void addView(View child, int index) {
		if (mContentLayout != null) {
			mContentLayout.addView(child, index);
		}
	}

	/**
	 * 设置头部内容数据
	 *
	 * @param adapter
	 */
	public void setAdapter(RecyclerView.Adapter adapter) {
		if (mHeaderView != null) {
			mHeaderView.setAdapter(adapter);
		}
	}


	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		//1.判断是上滑还是下拉
		float y = ev.getY();
		switch (ev.getAction()) {
			case MotionEvent.ACTION_DOWN:
				mLastY = y;
				break;
			case MotionEvent.ACTION_MOVE:
				int height = mHeaderView.getHeight();
				//解决滑动冲突
				if (mLastY >= mHeaderView.getTop() && mLastY <= mHeaderView.getBottom()) {
					return super.dispatchTouchEvent(ev);
				}

				int dy = (int) (y - mLastY);//与上一次滑动的距离
				mLastY = y;
				if (dy > 0) {
					//下拉
					mHeaderReducing = false;
					if (isScrollToTop && height < mHeaderMaxHeight) {
						ViewGroup.LayoutParams layoutParams = mHeaderView.getLayoutParams();
						layoutParams.height = height + dy;
						mHeaderView.setLayoutParams(layoutParams);
						mHeaderView.getAdapter().notifyDataSetChanged();
						mHeaderAdding = true;
						return false;
					} else {
						if (mHeaderAdding) {
							//滚动到头部最大
							statusCallBack(Status.MAX_STATUS);
						}
						//解决上滑跳动问题
						if (isScrollToTop) {
							return false;
						}

					}
				} else {
					//上滑
					mHeaderAdding = false;
					if (height > mHeaderMinHeight) {
						ViewGroup.LayoutParams layoutParams = mHeaderView.getLayoutParams();
						layoutParams.height = height + dy;
						mHeaderView.setLayoutParams(layoutParams);
						mHeaderView.getAdapter().notifyDataSetChanged();
						mHeaderReducing = true;
						return false;
					} else {
						if (mHeaderReducing) {
							//滚动到头部最小
							statusCallBack(Status.MIN_STATUS);
						}
					}

				}

				break;
			case MotionEvent.ACTION_UP:
				//处理松手回弹
				int headerHeight = mHeaderView.getHeight();
				if (headerHeight > mHeaderMinHeight && headerHeight < mHeaderMaxHeight) {
					//需要处理
					int targetHeight = (mHeaderMaxHeight - mHeaderMinHeight) / 2 + mHeaderMinHeight;
					if (headerHeight > targetHeight) {
						//回到header最大状态
						changeHeaderHeight(mHeaderMaxHeight);
						statusCallBack(Status.MAX_STATUS);
					} else {
						//回到header最小状态
						changeHeaderHeight(mHeaderMinHeight);
						statusCallBack(Status.MIN_STATUS);
					}
				}
				break;
		}
		return super.dispatchTouchEvent(ev);
	}

	/**
	 * 状态变化
	 *
	 * @param status
	 */
	private void statusCallBack(@Status int status) {
		switch (status) {
			case Status.MAX_STATUS:
				//滚动到头部最大
				mHeaderAdding = false;
				if (mScrollListener != null && mCurrentStatus != status) {
					mScrollListener.onHeaderScrollToMax();
				}

				break;

			case Status.MIN_STATUS:
				//滚动到头部最小
				mHeaderReducing = false;
				if (mScrollListener != null && mCurrentStatus != status) {
					mScrollListener.onHeaderScrollToMin();
				}
				break;
		}

		mCurrentStatus = status;

	}

	private void changeHeaderHeight(int height) {
		if (mHeaderView == null) return;
		final ViewGroup.LayoutParams layoutParams = mHeaderView.getLayoutParams();
		ValueAnimator valueAnimator = ValueAnimator.ofInt(mHeaderView.getHeight(), height);
		valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				int value = (int) animation.getAnimatedValue();
				layoutParams.height = value;
				mHeaderView.setLayoutParams(layoutParams);
				mHeaderView.getAdapter().notifyDataSetChanged();
			}
		});
		valueAnimator.start();
	}


	/**
	 * 设置滚动监听
	 *
	 * @param scrollListener
	 */
	public void setScrollListener(MyScrollListener scrollListener) {
		mScrollListener = scrollListener;
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		isScrollToTop = getScrollY() == 0;
	}


	@IntDef(flag = true, value = {
			Status.MIN_STATUS,
			Status.MAX_STATUS
	})
	@interface Status {
		int MIN_STATUS = 0;//头部最小状态
		int MAX_STATUS = 1;//头部最大状态
	}


}
